# -*- coding: utf-8 -*-

'''
/**
 * @file        bsc_shell_echo_parsing.py
 * @brief       XXXX
 * @note        XXXX
 * @author      靳普诏(puzhao.jin@hopechart.com)
 * @date        2024/08/03
 * @version     1.0

 * @par         修改日志
 * <table>
 * <tr><th>Date         <th>Version     <th>Author      <th> Description
 * <tr><td>2024/08/03   <td>1.0         <td>靳普诏       <td> 创建初始版本
 * @copyright   xxxx
 */
'''

import sys
import os
import argparse
import re

from datetime import datetime
# 获取当前日期
CURR_DATE_STRING = datetime.now().strftime("%Y-%m-%d")
# 获取当前时间
CURR_TIME_STRING = datetime.now().strftime("%H:%M:%S")
## 获取当前脚本所在的目录
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
# 输出目录路径
# print("SCRIPT_DIR: "+ SCRIPT_DIR)




def BsShellEchoParsingGetIntValue(src_str:str, header_pattern:str = '') -> int:
    pattern = header_pattern + " ?[-]?[0-9]{1,10}"
    targ_str = re.search(pattern, src_str)
    if targ_str is None:
        raise Exception(f"{pattern} not found in {src_str}")
    else:
        matched_text = targ_str.group()
        if (header_pattern == ''):
            return int(matched_text)
        else:
            return BsShellEchoParsingGetIntValue(matched_text)


def BsShellEchoParsingGetStrValue(src_str:str, header_pattern:str = '', end_pattern:str = '\n') -> str:

    ret_start_index = 0
    ret_end_index = len(src_str)

    if (header_pattern != ''):
        temp = src_str.find(header_pattern)
        if temp >= 0:
            ret_start_index = temp + len(header_pattern)
        else:
            raise Exception(f"{header_pattern} not found in {src_str}")

    if (end_pattern != ''):
        temp = src_str[ret_start_index:].find(end_pattern)
        if temp >= 0:
            ret_end_index = temp + ret_start_index
        else:
            raise Exception(f"{end_pattern} not found in {src_str[ret_start_index:]}")
    
    return src_str[ret_start_index:ret_end_index]


if __name__ == "__main__":


    ## 获取脚本传参
    argv = sys.argv
    argc = len(argv)
    # 打印命令行参数
    #for i in range(1, argc):
        #print("参数", i, ":", argv[i])


    # 设置命令行参数解析器
    # parser = argparse.ArgumentParser(description='Filter log file based on keywords.')
    # parser.add_argument('input_file', type=str, help='Input log file path')
    # parser.add_argument('output_file', type=str, help='Output filtered log file path')
    # parser.add_argument('filter_list', nargs='+', type=str, help='List of keywords to filter')

    # 解析命令行参数
    # args = parser.parse_args()

        
    print("============= BIGEN: bsc_shell_echo_parsing.py =============")

    # pattern = re.compile(r'123')
    # result = pattern.search('abc123def456')
    # print("编译后的查找结果:", result)


    # test_int = BsShellEchoParsingGetIntValue("Bin id=339, ret: 11, t_ret:-2146926504, t_stamp:0", "id=")
    # print(f"test_int: {test_int}")

    # test_int = BsShellEchoParsingGetIntValue("Bin id=339, ret: 11, t_ret:-2146926504, t_stamp:0", "t_stamp:")
    # print(f"test_int: {test_int}")

    # test_int = BsShellEchoParsingGetIntValue("Bin id=339, ret: 11, t_ret:-2146926504, t_stamp:0", "ret:")
    # print(f"test_int: {test_int}")

    # test_int = BsShellEchoParsingGetIntValue("Bin id=339, ret: 11, t_ret:-2146926504, t_stamp:0", "t_ret:")
    # print(f"test_int: {test_int}")

    test_str = BsShellEchoParsingGetStrValue("Bin id=339, ret: 11, t_ret:-2146926504, t_stamp:0", "Bin id=", "ret:")
    print(f"test_str: {test_str}")

    test_str = BsShellEchoParsingGetStrValue("Bin id=339, ret: 11, t_ret:-2146926504, t_stamp:0", 
                                            "t: 1", 
                                            "am")
    print(f"test_str: {test_str}")

    print("============= END: bsc_shell_echo_parsing.py =============")



## Generated on "2024-08-03 14:10:14" by the tool "gen_hq_file.py >> V20231119" 

